﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace PeekService
{
    /// <summary>
    /// 购票信息数据库服务类
    /// </summary>
    public class TicketInfoService
    {
        /// <summary>
        /// 增加购票信息
        /// </summary>
        public bool Add(TicketInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into TicketInfo(");
            strSql.Append("Code,TicketType,TourismCode,TicketFare,Seat,SoldSeat,PriceUnit,EffectiveDate,Currency,Remark,State,CreateID,CreateTime,Sort)");
            strSql.Append(" values (");
            strSql.Append("@Code,@TicketType,@TourismCode,@TicketFare,@Seat,@SoldSeat,@PriceUnit,@EffectiveDate,@Currency,@Remark,@State,@CreateID,@CreateTime,@Sort)");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,36),
					new SqlParameter("@TicketType", SqlDbType.Int,4),
					new SqlParameter("@TourismCode", SqlDbType.VarChar,36),
					new SqlParameter("@TicketFare", SqlDbType.Decimal,9),
					new SqlParameter("@Seat", SqlDbType.Int,4),
					new SqlParameter("@SoldSeat", SqlDbType.Int,4),
					new SqlParameter("@PriceUnit", SqlDbType.NVarChar,50),
					new SqlParameter("@EffectiveDate", SqlDbType.DateTime),
					new SqlParameter("@Currency", SqlDbType.VarChar,20),
					new SqlParameter("@Remark", SqlDbType.NVarChar,500),
					new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@CreateID", SqlDbType.VarChar,36),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Sort", SqlDbType.Int,4)};
            parameters[0].Value = model.Code;
            parameters[1].Value = model.TicketType;
            parameters[2].Value = model.TourismCode;
            parameters[3].Value = model.TicketFare;
            parameters[4].Value = model.Seat;
            parameters[5].Value = model.SoldSeat;
            parameters[6].Value = model.PriceUnit;
            parameters[7].Value = model.EffectiveDate;
            parameters[8].Value = model.Currency;
            parameters[9].Value = model.Remark;
            parameters[10].Value = model.State;
            parameters[11].Value = model.CreateID;
            parameters[12].Value = model.CreateTime;
            parameters[13].Value = model.Sort;

            int rows = DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 更新购票信息
        /// </summary>
        public bool Update(TicketInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update TicketInfo set ");
            strSql.Append("TicketType=@TicketType,");
            strSql.Append("TourismCode=@TourismCode,");
            strSql.Append("TicketFare=@TicketFare,");
            strSql.Append("Seat=@Seat,");
            strSql.Append("SoldSeat=@SoldSeat,");
            strSql.Append("PriceUnit=@PriceUnit,");
            strSql.Append("EffectiveDate=@EffectiveDate,");
            strSql.Append("Currency=@Currency,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("State=@State,");
            strSql.Append("CreateID=@CreateID,");
            strSql.Append("CreateTime=@CreateTime,");
            strSql.Append("Sort=@Sort");
            strSql.Append(" where Code=@Code ");
            SqlParameter[] parameters = {
					new SqlParameter("@TicketType", SqlDbType.Int,4),
					new SqlParameter("@TourismCode", SqlDbType.VarChar,36),
					new SqlParameter("@TicketFare", SqlDbType.Decimal,9),
					new SqlParameter("@Seat", SqlDbType.Int,4),
					new SqlParameter("@SoldSeat", SqlDbType.Int,4),
					new SqlParameter("@PriceUnit", SqlDbType.NVarChar,50),
					new SqlParameter("@EffectiveDate", SqlDbType.DateTime),
					new SqlParameter("@Currency", SqlDbType.VarChar,20),
					new SqlParameter("@Remark", SqlDbType.NVarChar,500),
					new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@CreateID", SqlDbType.VarChar,36),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Sort", SqlDbType.Int,4),
					new SqlParameter("@Code", SqlDbType.VarChar,36)};
            parameters[0].Value = model.TicketType;
            parameters[1].Value = model.TourismCode;
            parameters[2].Value = model.TicketFare;
            parameters[3].Value = model.Seat;
            parameters[4].Value = model.SoldSeat;
            parameters[5].Value = model.PriceUnit;
            parameters[6].Value = model.EffectiveDate;
            parameters[7].Value = model.Currency;
            parameters[8].Value = model.Remark;
            parameters[9].Value = model.State;
            parameters[10].Value = model.CreateID;
            parameters[11].Value = model.CreateTime;
            parameters[12].Value = model.Sort;
            parameters[13].Value = model.Code;

            int rows = DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string Code)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from TicketInfo ");
            strSql.Append(" where Code=@Code ");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,36)			};
            parameters[0].Value = Code;

            int rows =DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Codelist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from TicketInfo ");
            strSql.Append(" where Code in (" + Codelist + ")  ");
            int rows =DBHelper.ExecuteNonQuery(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public  TicketInfo DataRowToModel(SqlDataReader reader)
        {
            TicketInfo model = new TicketInfo();
            
                if (reader["Code"] != DBNull.Value)
                {
                    model.Code = reader["Code"].ToString();
                }

                if (reader["TourismCode"] != DBNull.Value)
                {
                    model.TourismCode = reader["TourismCode"].ToString();
                }
                if (reader["TicketType"] != DBNull.Value)
                {
                    model.TicketType = int.Parse(reader["TicketType"].ToString());
                }
                if (reader["TicketFare"] != DBNull.Value)
                {
                    model.TicketFare =decimal.Parse(reader["TicketFare"].ToString());
                }
                if (reader["Seat"] != DBNull.Value)
                {
                    model.Seat = int.Parse(reader["Seat"].ToString());
                }
                if (reader["SoldSeat"] != DBNull.Value)
                {
                    model.SoldSeat = int.Parse(reader["SoldSeat"].ToString());
                }
                if (reader["PriceUnit"] != DBNull.Value)
                {
                    model.PriceUnit = reader["PriceUnit"].ToString();
                }
                if (reader["EffectiveDate"] != DBNull.Value)
                {
                    model.EffectiveDate =DateTime.Parse(reader["EffectiveDate"].ToString());
                }
                if (reader["Currency"] != DBNull.Value)
                {
                    model.Currency = reader["Currency"].ToString();
                }
                if (reader["Remark"] != DBNull.Value)
                {
                    model.Remark = reader["Remark"].ToString();
                }
                if (reader["State"] != null && reader["State"].ToString() != "")
                {
                    model.State = int.Parse(reader["State"].ToString());
                }
                if (reader["CreateID"] != DBNull.Value)
                {
                    model.CreateID = reader["CreateID"].ToString();
                }
                if (reader["CreateTime"] != DBNull.Value && reader["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(reader["CreateTime"].ToString());
                }
                if (reader["Sort"] != DBNull.Value && reader["Sort"].ToString() != "")
                {
                    model.Sort = int.Parse(reader["Sort"].ToString());
                }
            
            return model;
        }

    }
}
